package com.plian.system.service.transfer.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.DateUtil;
import com.plian.system.dto.transfer.DataPlatSign;
import com.plian.system.entity.fr.bank.BankKey;
import com.plian.system.entity.qg.guarantee.PropertyGuaranteeApply;
import com.plian.system.entity.qg.guarantee.PropertyGuaranteeExecute;
import com.plian.system.entity.sys.Org;
import com.plian.system.model.R;
import com.plian.system.service.fr.bank.IBankKeyService;
import com.plian.system.service.qg.guarantee.IPropertyGuaranteeApplyService;
import com.plian.system.service.qg.guarantee.IPropertyGuaranteeExecuteService;
import com.plian.system.service.sys.orgzation.IOrgService;
import com.plian.system.service.transfer.IPropertyGuaranteeService;
import com.plian.system.vo.qg.guarantee.PropertyGuaranteeExecuteVO;
import com.plian.system.wrapper.qg.guarantee.PropertyGuaranteeExecuteWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Service
@AllArgsConstructor
public class PropertyGuaranteeServiceImpl implements IPropertyGuaranteeService {

    private IBankKeyService bankKeyService;

    private IOrgService orgService;

    private IPropertyGuaranteeApplyService propertyGuaranteeApplyService;

    private IPropertyGuaranteeExecuteService propertyGuaranteeExecuteService;

    private PropertyGuaranteeExecuteWrapper executeWrapper;

    /**
     * 提供时间段内的企业担保申请-数据接口
     * @param dataPlatSign
     */
    @Override
    public R<List<PropertyGuaranteeApply>> getPropertyGuaranteeApply( DataPlatSign dataPlatSign){
        log.debug("toklen:" + dataPlatSign.getToken());
        String appId = dataPlatSign.getAppId();
        BankKey bankKey = bankKeyService.getOne(Wrappers.<BankKey>lambdaQuery().eq(BankKey::getAppId, appId));
        String topGroupId = bankKey.getTopGroupId();
        List<Org> allChild = orgService.getAllChild(topGroupId);
        List<String> orgIds = allChild.stream().map(Org::getId).collect(Collectors.toList());
        List<PropertyGuaranteeApply> list = new ArrayList<>();
        if (Optional.ofNullable(dataPlatSign.getEndTime()).isPresent()){
            String startTime = DateUtil.format(dataPlatSign.getStartTime(), DateUtil.PATTERN_DATE);
            String endTime = DateUtil.format(DateUtil.minusMillis(DateUtil.plusDays(dataPlatSign.getEndTime(), 1), 1), DateUtil.PATTERN_DATE);
            list = propertyGuaranteeApplyService.list(new LambdaQueryWrapper<PropertyGuaranteeApply>().eq(PropertyGuaranteeApply::getStatus, 3).in(PropertyGuaranteeApply::getApplyNameId, orgIds)
                    .eq(PropertyGuaranteeApply::getIsDeleted, 0).between(PropertyGuaranteeApply::getApplyDate, startTime, endTime));

        }else {
            list = propertyGuaranteeApplyService.list(new LambdaQueryWrapper<PropertyGuaranteeApply>().in(PropertyGuaranteeApply::getApplyNameId, orgIds)
                    .eq(PropertyGuaranteeApply::getStatus,3));
        }
        return R.data(list);
    }

    /**
     * @Description: 提供时间段内的企业担保执行-数据接口
     * @Author: Take-off
     * @Date: 4:41 PM 2023/4/12
     * @Param: [dataPlatSign]
     * @return: com.plian.system.model.R<java.util.List<com.plian.system.entity.qg.guarantee.PropertyGuaranteeExecute>>
     **/
    @Override
    public R<List<PropertyGuaranteeExecuteVO>> getPropertyGuaranteeExecute( DataPlatSign dataPlatSign){
        log.debug("toklen:" + dataPlatSign.getToken());
        String appId = dataPlatSign.getAppId();
        BankKey bankKey = bankKeyService.getOne(Wrappers.<BankKey>lambdaQuery().eq(BankKey::getAppId, appId));
        String topGroupId = bankKey.getTopGroupId();
        List<Org> allChild = orgService.getAllChild(topGroupId);
        List<String> orgIds = allChild.stream().map(Org::getId).collect(Collectors.toList());
        List<PropertyGuaranteeExecuteVO> propertyGuaranteeExecuteVOS = new ArrayList<>();
        List<PropertyGuaranteeExecute> list = new ArrayList<>();
        if (Optional.ofNullable(dataPlatSign.getEndTime()).isPresent()){
            String startTime = DateUtil.format(dataPlatSign.getStartTime(), DateUtil.PATTERN_DATE);
            String endTime = DateUtil.format(DateUtil.minusMillis(DateUtil.plusDays(dataPlatSign.getEndTime(), 1), 1), DateUtil.PATTERN_DATE);
            list = propertyGuaranteeExecuteService.list(new LambdaQueryWrapper<PropertyGuaranteeExecute>().eq(PropertyGuaranteeExecute::getStatus, 3).in(PropertyGuaranteeExecute::getApplyNameId, orgIds)
                    .eq(PropertyGuaranteeExecute::getIsDeleted, 0).between(PropertyGuaranteeExecute::getCreateTime, startTime, endTime));
        }else {
            list = propertyGuaranteeExecuteService.list(new LambdaQueryWrapper<PropertyGuaranteeExecute>().in(PropertyGuaranteeExecute::getApplyNameId, orgIds)
                    .eq(PropertyGuaranteeExecute::getStatus,3));
        }
        if (CollectionUtil.isNotEmpty(list)){
             propertyGuaranteeExecuteVOS = executeWrapper.entityToVO(list);
            List<PropertyGuaranteeApply> applyList = propertyGuaranteeApplyService.list(new LambdaQueryWrapper<PropertyGuaranteeApply>().in(PropertyGuaranteeApply::getId,
                    list.stream().map(PropertyGuaranteeExecute::getUpstreamCodeId).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())));
            if (CollectionUtil.isNotEmpty(applyList)){
                for (PropertyGuaranteeExecuteVO vo:propertyGuaranteeExecuteVOS){
                    applyList.stream().filter(m -> vo.getUpstreamCodeId().equals(m.getId())).findFirst().ifPresent(propertyGuaranteeApply ->{
                        vo.setGuaranteeRemain(propertyGuaranteeApply.getGuaranteeRemain()==null?new BigDecimal(0):propertyGuaranteeApply.getGuaranteeRemain());
                    });
                }
            }

        }
        return R.data(propertyGuaranteeExecuteVOS);
    }
}
